package data.composition.factory.impl;

import cn.hutool.core.lang.func.Func1;
import data.composition.factory.core.DataComposition;
import data.composition.factory.core.MapObj;
import data.composition.factory.core.Mapping;
import data.composition.factory.core.MappingInner;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

/**
 * @author ZhangJinyu
 * @since 2024-12-13
 */
public class MappingImpl<T, R, V> implements MappingInner<T, R, V> {
    private final DataComposition<T> dataComposition;
    private final Function<T, V> dk;
    private final List<MapObj> mapObjs;

    public MappingImpl(DataComposition<T> dataComposition, Function<T, V> dk) {
        this.dataComposition = dataComposition;
        this.dk = dk;
        this.mapObjs = new LinkedList<>();
    }

    @Override
    public Function<T, V> getDataFunction() {
        return dk;
    }

    @Override
    public List<MapObj> getMapObjs() {
        return mapObjs;
    }

    @Override
    public Mapping<T, R, V> map(Func1<T, R> left) {
        mapObjs.add(new MapObj(left, o -> o));
        return this;
    }

    @Override
    public <RV> Mapping<T, R, V> map(Func1<T, RV> left, Function<R, RV> right) {
        this.mapObjs.add(new MapObj(left, right));
        return this;
    }

    @Override
    public DataComposition<T> over() {
        return dataComposition;
    }
}
